package com.qiwx.pigeon.Utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * @author: voiceofnet
 * email: pengkun@jingzhengu.com
 * phone:18101032717
 * @time: 2017/4/20 09:14
 * @desc:
 */
public class DateTimeUtils {

    public enum TimeType {
        YEAR, MONTH, DAY
    }

    public static int getCurrent(TimeType type) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        int result = 0;
        switch (type) {
            case YEAR:
                result = calendar.get(Calendar.YEAR);
                break;
            case MONTH:
                result = calendar.get(Calendar.MONTH) + 1;
                break;
            case DAY:
                result = calendar.get(Calendar.DAY_OF_MONTH);
                break;
        }
        return result;
    }

    /**
     * 2015-12  --> 2015
     */
    public static int getYear4String(String yearStr) {
        int year = 0;
        try {
            if ("".equals(yearStr) || null == yearStr) {
                year = 0;
            } else {
                if (yearStr.indexOf("-") != -1) {
                    year = Integer.parseInt(yearStr.substring(0,
                            yearStr.indexOf("-")));
                } else {
                    year = 0;
                }
            }
        } catch (Exception e) {
            year = 0;
        }
        return year;
    }


    /**
     * 2015-03  --> 3
     */
    public static int getMonth4String(String yearStr) {
        int year = 0;
        try {
            if ("".equals(yearStr) || null == yearStr) {
                year = 0;
            } else {
                if (yearStr.indexOf("-") != -1) {
                    year = Integer.parseInt(yearStr.substring(yearStr.indexOf("-") + 1));
                } else {
                    year = 0;
                }
            }
        } catch (Exception e) {
            year = 0;
        }
        return year;
    }

    //date 转字符串
    public static String date2String(Date date) {
        if (date == null)
            date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(date);
    }

    //字符串转Date
    public static Date string2Date(String date) {
        if (date == null)
            return new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return simpleDateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new Date();
    }

    //
    public static Date addYear(Date date, int year) {
        return add(date, Calendar.YEAR, year);
    }

    //将date日期往前或者向后增加日期
    public static Date addDay(Date date, int days) {
        return add(date, Calendar.DATE, days);
    }

    // amount 具体值 正数向后 负数向前 filed为单位 天 月 年
    public static Date add(Date date, int filed, int amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(filed, amount);
        return calendar.getTime();
    }

    /**
     * 取得当前时间戳（精确到秒）
     *
     * @return
     */
    public static String timeStamp() {
        long time = System.currentTimeMillis();
        String t = String.valueOf(time / 1000);
        return t;
    }

    /**
     * 时间戳转换成日期格式字符串
     *
     * @param seconds 精确到秒的字符串
     * @param
     * @return
     */
    public static String timeStamp2Date(long seconds, String format) {
        if (seconds < 0) {
            return "";
        }
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(seconds));
    }

    public static String timeStamp2Date(long seconds) {
        return timeStamp2Date(seconds, null);
    }

    public static String timeStamp2DateTime(long seconds) {
        return timeStamp2Date(seconds, "yyyy-MM-dd");
    }


    /**
     * 日期格式字符串转换成时间戳
     *
     * @param
     * @param format 如：yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static long date2TimeStamp(String date_str, String format) {
        try {
            if (format == null)
                format = "yyyy-MM-dd";
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.parse(date_str).getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    //获取当前时间 时分秒
    public static String getCurrentTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(new Date());

    }

    public static long date2TimeStamp(String date_str) {

        return date2TimeStamp(date_str, "yyyy-MM-dd");
    }


    public static long getCurrentStamp() {
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        return date2TimeStamp(sdf.format(new Date()));
        return new Date().getTime();
    }

    //计算相差毫秒至可能会忽略自然天
    public static int differentDaysByMillisecond(Date date1, Date date2) {
        int days = (int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24));
        return days;
    }

    //计算相差毫秒 分钟
    public static int differentMinute(Date date1, Date date2) {
        int days = (int) ((date2.getTime() - date1.getTime()) / (1000 * 60));
        return days;
    }


    /**
     * date2比date1多的天数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDays(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if (year1 != year2)   //同一年
        {
            int timeDistance = 0;
            for (int i = year1; i < year2; i++) {
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)    //闰年
                {
                    timeDistance += 366;
                } else    //不是闰年
                {
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2 - day1);
        } else {
            System.out.println("判断day2 - day1 : " + (day2 - day1));
            return day2 - day1;
        }
    }

    //转换月份和日期 Month Day
    public static String timeStamp2MDS(String datetime) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");

        try {
            return sdf.format(sdf1.parse(datetime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String timeStamp2MDS(long timestamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日");
        return sdf.format(new Date(timestamp));
    }

    public static String timeStamp2MDHMSS(long timestamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm:ss");
        return sdf.format(new Date(timestamp));
    }

    //添加时间后缀 00:00:00 或者 23:59:59
    public static String addTimeSuffix(String date, boolean isMax) {
        return date;
        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        if (isMax) {
//            date = date + " 23:59:59";
//        } else {
//            date = date + " 00:00:00";
//        }
//        Date date1 = null;
//        try {
//            date1 = sdf.parse(date);
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
        // return date1 != null ? date1.getTime() : getCurrentStamp();
    }

    public static String addTimeSuffixNew(String date, boolean isMax) {
        // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        if (isMax) {
//            date = date + " 23:59:59";
//        } else {
//            date = date + " 00:00:00";
//        }
        return date;
    }

    //mysql中datetime会多个.0
    public static String formatDateTime(String date_time) {
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dd = null;
        try {
            dd = sdf.parse(date_time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (dd == null) {
            dd = new Date();
        }
        return sdf.format(dd);
    }
    public static String formatDate(String date_time,String format) {
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dd = null;
        try {
            dd = sdf.parse(date_time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (dd == null) {
            dd = new Date();
        }
        SimpleDateFormat sdf2 = new SimpleDateFormat(format);
        return sdf2.format(date_time);
    }

    //去掉数据库datetime后缀.0
    public static String showOperateTime(String opereate_time) {
        if (opereate_time != null && opereate_time.contains(".0")) {
            opereate_time = opereate_time.replace(".0", "");
        }
        return opereate_time;
    }

    //查询下蛋日期距离现在多少天
    public static int getLayEggDaysFromToday(String d1) {
        if (d1 == null || d1.length() <= 0) {
            return 0;
        }
        String d2 = getCurrentDateStr();
        return differentDays(d1, d2);

    }

    public static int differentDays(String d1, String d2) {
        Date date1;
        Date date2;
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date1 = sdf.parse(d1);
            date2 = sdf.parse(d2);
            return differentDays(date1, date2);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static String getCurrentDateStr() {

        return timeStamp2Date(new Date().getTime(), "yyyy-MM-dd");
    }

    //获取当前月份天数
    public static int getCurrentMonthDays() {
        Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
        int days = aCalendar.getActualMaximum(Calendar.DATE);
        return days;
    }

}
